<?php

namespace App\Http\Controllers\Back;

use App\Model\User;
use App\Model\Role;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Log;
use App\Util\TimeHelper;

class UserController extends Controller
{

    private function leftBar() {
        return [
            'title' => '管理员管理',
            'list'  => [
                [
                    'title' => '管理员列表',
                    'type' => 'userlist',
                    'url' => url('back/user'),
                ],
                [
                    'title' => '添加管理员',
                    'type' => 'adduser',
                    'url' => url('back/user/create'),
                ],
//                [
//                    'title' => '角色列表',
//                    'type' => 'rolelist',
//                    'url' => url('admin/role'),
//                ],
//                [
//                    'title' => '添加角色',
//                    'type' => 'addrole',
//                    'url' => url('admin/role/create'),
//                ],
//                [
//                    'title' => '节点列表',
//                    'type' => 'nodelist',
//                    'url' => url('admin/node'),
//                ],
//                [
//                    'title' => '添加节点',
//                    'type' => 'addnode',
//                    'url' => url('admin/node/create'),
//                ],
            ]

        ];
    }

    public function __construct(Request $request){

        $this->request = $request;
        $this->limit = config('config.pageLimit');
        $leftBarArr = $this->leftBar();
        view()->share(['title' => $leftBarArr['title'],'list'=>$leftBarArr['list'],'limit'=>$this->limit]);
    }

    /**
     * Display a listing of the resource.
     * 管理员列表页
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $keyWord = $this->request->input('keyword');
        $res = User::getList($keyWord,$this->limit)->getData();
        $this->request->flash();
        return view('back/user/index')
            ->with('res',$res)
            ->with('titleType','userlist');
    }

    /**
     * Show the form for creating a new resource.
     * 管理员创建保存
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if($this->request->isMethod('post')) {
            $input = $this->request->all();
        	$res = User::getByAccount($input["account"])->getData();
            if (!$res->isEmpty()) {
            	$return = array(
            			'success' => false,
            			'msg' => "添加失败，账号已经存在，请重试！"
            	);
            	return $return;
            }
            $input['password'] = bcrypt($input['password']);
            $input['status'] = 1;
            $res = User::create($input);
            if ($res) {
                $return = array(
                    'success' => true,
                    'msg' => "添加成功！"
                );
            } else {
                $return = array(
                    'success' => false,
                    'msg' => "添加失败，请重试！"
                );
            }
            return $return;
        } else {
            $res = Role::roleList()->getData();
            return view('back/user/create')
                ->with('res',$res)
                ->with('titleType','adduser');
        }

    }

    /**
     * Show the form for editing the specified resource.
     * 管理员编辑保存
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if($this->request->isMethod('post')) {
            $input = $this->request->all();
            if (!empty($input['password'])) {
                $input['password'] = bcrypt($input['password']);
            } else {
                $input = $this->request->except('password');
            }
            $input['status'] = 1;
            $res = User::updateUser($input, $id);
            return $res->getResult();
        } else {
            $res = Role::roleList()->getData();
            $user = User::getOne($id)->getData();
            return view('back/user/edit')
                ->with('res',$res)
                ->with('user', $user);
        }
    }

    /**
     * Remove the specified resource from storage.
     * 删除user
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        $del = User::deleteUser($id);
        return $del->getResult();
    }

    /**
     * @param $id
     * 禁用、启用
     */
    public function able($id,$able)
    {
        $res = User::able($id,$able)->getData();
        if($res) {
            return redirect('admin/user');
        }
    }
}
